//
// Created by 86186 on 2021/12/8.
//

#include "AVPacketQueue.h"

AVPacketQueue::AVPacketQueue(mutex &qMutex, condition_variable &cv) : qMutex(qMutex), cv(cv) {}

int AVPacketQueue::push(AVPacket *avPacket) {
    lock_guard<mutex> guard(this->qMutex);
    this->queue.push(avPacket);
    cv.notify_all();
    return this->queue.size();
}

AVPacket *AVPacketQueue::pop() {
    unique_lock<mutex> lk(this->qMutex);
    cv.wait(lk, [this]() {
        return this->queue.size() > 0;
    });
    AVPacket *front = this->queue.front();
    this->queue.pop();
    return front;
}

int AVPacketQueue::size() {
    return queue.size();
}
